package com.ytzl.itrip.biz.controller;

import com.ytzl.itrip.beans.model.ItripImage;
import com.ytzl.itrip.beans.model.ItripLabelDic;
import com.ytzl.itrip.beans.model.vo.HotelRoomVo;
import com.ytzl.itrip.beans.model.vo.ItripHotelRoomVo;
import com.ytzl.itrip.beans.model.vo.ItripImageVo;
import com.ytzl.itrip.beans.model.vo.ItripLabelDicVo;
import com.ytzl.itrip.biz.service.ItripHotelRoomService;
import com.ytzl.itrip.biz.service.ItripImageService;
import com.ytzl.itrip.biz.service.ItripLabelDicService;
import com.ytzl.itrip.utils.common.Dto;
import com.ytzl.itrip.utils.common.DtoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/hotelroom")
public class HotelRoomController {

    @Resource
    private ItripImageService itripImageService;
    @Resource
    private ItripLabelDicService itripLabelDicService;
    @Resource
    private ItripHotelRoomService itripHotelRoomService;

    @RequestMapping(value = "/getimg/{targetId}",method = RequestMethod.GET,
            produces = "application/json")
    @ResponseBody
    public Dto getimg(@PathVariable("targetId") String targetId){
        Map map = new HashMap();
        map.put("type","1");
        if (targetId==null && targetId==""){
            return DtoUtil.returnFail("酒店房型id不能为空","100302");
        }else{
            map.put("targetId",targetId);
            List<ItripImageVo> itripImageVos = new ArrayList<>();
            try {
                List<ItripImage> itripImages = itripImageService.getListByMap(map);
                for (ItripImage itripImage:itripImages){
                    ItripImageVo itripImageVo = new ItripImageVo();
                    BeanUtils.copyProperties(itripImage,itripImageVo);
                    itripImageVos.add(itripImageVo);
                }
                return DtoUtil.returnSuccess("获取酒店房型图片成功",itripImageVos);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("获取酒店房型图片失败","100301");
            }
        }
    }


    @RequestMapping(value = "/queryhotelroombed",method = RequestMethod.GET)
    @ResponseBody
    public Dto queryhotelroombed(){
        Map map = new HashMap();
        map.put("parentId",1);
        List<ItripLabelDicVo> itripLabelDicVos = new ArrayList<>();
        try {
            List<ItripLabelDic> itripLabelDics = itripLabelDicService.getListByMap(map);
            for (ItripLabelDic itripLabelDic:itripLabelDics){
                ItripLabelDicVo itripLabelDicVo = new ItripLabelDicVo();
                BeanUtils.copyProperties(itripLabelDic,itripLabelDicVo);
                itripLabelDicVos.add(itripLabelDicVo);
            }
            return DtoUtil.returnSuccess("获取成功",itripLabelDicVos);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("获取酒店房间床型失败","100305");
        }
    }

    @RequestMapping(value = "/queryhotelroombyhotel",method = RequestMethod.POST)
    @ResponseBody
    public Dto queryhotelroombyhotel(@RequestBody HotelRoomVo hotelRoomVo){
        if (hotelRoomVo.getHotelId()==null){
            return DtoUtil.returnFail("酒店id不能为空","100303");
        }else if (hotelRoomVo.getStartDate()==null || hotelRoomVo.getEndDate()==null){
            return DtoUtil.returnFail("必须填写酒店入住及退房时间","100303");
        }else{
            List listAll = new ArrayList();
            if (hotelRoomVo.getRoomBedTypeId()!=null&&hotelRoomVo.getRoomBedTypeId()>4)hotelRoomVo.setRoomBedTypeId(null);
            try {
                List<ItripHotelRoomVo> itripHotelRoomVos = itripHotelRoomService.getRoomList(hotelRoomVo);
                for (int i=0;i<itripHotelRoomVos.size();i++){
                    List list = new ArrayList();
                    ItripHotelRoomVo itripHotelRoomVo = itripHotelRoomVos.get(i);
                    list.add(itripHotelRoomVo);
                    listAll.add(list);
                }
                return DtoUtil.returnSuccess("获取成功",listAll);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("系统异常","100304");
            }
        }
    }


}
